<?php
/**
 * Created by PhpStorm.
 * User: 刘广
 * Date: 2018/5/24
 * Time: 17:47
 */

namespace app\index\controller\love;

use app\index\controller\Love;
use think\Controller;
use app\index\controller\Weixin;
use think\Db;

class Activity extends Love
{
    /**
     * 爱心司机认证主页（GET）
     * URL：index/love.activity/index
     */
    public function index()
    {
        $wx = session('uid');
        $data = Db::name('dd_activity_gk')
            ->order('uid desc')
            ->limit(12)
            ->select();
        $res = Db::name('dd_activity_gk')
            ->select();
        $number = count($res);
        $number +=100;
        $this->assign('number',$number);
        $this->assign('data',$data);
        return $this->fetch();
    }
    /**
     * 爱心司机认证详情页面
     * URL：index/love.activity/application
     */
    public function application()
    {
        if(request()->isAjax()){
            $data = input();
            $res = Db::name('sms_code')
                ->where('phone',$data['tel'])
                ->where('code',$data['code'])
                ->find();
            if($res){
                unset($data['code']);
                $data['update_time'] = time();
                $data['auth_status'] = 1;
                $result = Db::name('dd_activity_gk')->where('uid',$data['uid'])->update($data);
                if($result){
                    $this->success('信息提交成功');
                }else{
                    $this->error('信息提交失败');
                }
            }else{
                $this->error('手机验证码不正确');
            }
        }else{
            $wx = session('uid');
            $data = Db::name('dd_activity_gk')->where('uid',$wx)->find();
            $qiniuyun = "?imageMogr2/auto-orient/thumbnail/!160x120r/gravity/Center/crop/160x120/quality/100/interlace/0";
            $data['driver_license'] = $data['driver_license'].$qiniuyun;
            $data['vehicle_license'] = $data['vehicle_license'].$qiniuyun;
            //dump($data);die();
            $this->assign('data',$data);
            return $this->fetch();
        }

    }
    /**
     * 验证手机号码是否注册过（GET）
     * URL：index/love.activity/checkphone
     */
    public function checkphone()
    {
        $wx = session('uid');
        $data = input();
        $result = Db::name('dd_activity_gk')
            ->where('tel',$data['phone'])
            ->select();
        $number = count($result);
        //dump($number);die();
        if($number>1){
            //手机号码已被别人占用
            return $this->error("手机号码已占用");

        }elseif ($number == 1){
            // 判断是用户本人认证未通过
            $res =  Db::name('dd_activity_gk')
                ->where('uid',$wx)
                ->where('tel',$data['phone'])
                ->find();
            if($res){
                // 手机号为自己认证
                if ($res['auth_status'] == -1){
                    //  未通过认证
                    return $this->success();
                }elseif ($res['auth_status'] == 0){
                    // 首次认证
                    return $this->success();
                }
                else{
                    return $this->error("手机号码已注册过");
                }
            }else{
                // 手机号已经被占用
                return $this->error("手机号码已被人注册过");
            }

        } else{
            return $this->success();
        }

    }
    /**
     * 验证身份证有效性
     */
    public function checkIdCard()
    {
        $id_card = request()->post('id_card');
        if(!$id_card){
            $this->error("请填写正确的身份证号");
        }

        // 验证
        $res = check_id_card($id_card);
        if($res){
            return $this->success("身份证有效");
        }else{
            return $this->error("请填写正确的身份证号");
        }

    }
    /**
     * demo
     */
    public function demo()
    {
        $result = Db::name('dd_activity_gk')
            ->where('tel','15188501166')
            ->select();
        dump(count($result));die();
    }
}